package apIExercise.collection2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SorPoint {
    public static void main(String[] args) {
        List<Point> list=new ArrayList<>();
        list.add(new Point(1,80));
        list.add(new Point(60,5));
        list.add(new Point(20,40));
        list.add(new Point(50,15));
        System.out.println(list);

        ////jdk1.8时，List集合自身提供了sort方法进行排序，方法中需要传入比较器
        list.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                 return o1.getX()  - o2.getX() ;
            }
        });

        list.sort((Point o1, Point o2)-> {
                return o1.getX() * o1.getY() - o2.getX() * o2.getY();//升序
        });

        Collections.sort(list, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                //return o1.getX()-o2.getX();//按x坐标升序
                return o1.getY()-o2.getY();//按y坐标升序
//                return o2.getY()-o1.getY();降序按y坐标
//                return o2.getX()-o1.getX();降序按x坐标

            }
        });
        System.out.println(list+":排序后");

    }
}
